/**
 * fileName: DaoActivityRecordImpl.java
 * fileTime: 2011-6-17下午10:17:50
 */
package com.bf.xpdd.persistent.sqlmapdao;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.bf.xpdd.domain.ActivityRecordBean;
import com.bf.xpdd.domain.AnalysisDealByDay;
import com.bf.xpdd.domain.AnalysisDealByItem;
import com.bf.xpdd.domain.SiteSeller;
import com.bf.xpdd.persistent.iface.DaoActivityRecord;
import com.bf.xpdd.util.Constant;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * 成交记录实现
 * 
 * @author linmu
 * 
 */
public class DaoActivityRecordImpl implements DaoActivityRecord
{
	/**
	 * 日志记录器
	 */
	private Logger logger = Logger.getLogger(DaoActivityRecordImpl.class);

	/**
	 * ibatis的查询接口
	 */
	private SqlMapClient sqlMapClient;

	/**
	 * 注入sqlMapClient
	 * 
	 * @param sqlMapClient
	 */
	public void setSqlMapClient(SqlMapClient sqlMapClient)
	{
		this.sqlMapClient = sqlMapClient;
	}

	@Override
	public void addActivityRecord(ActivityRecordBean actRecord)
	{
		// 活动记录的id为0,这里用于传入当前月份
		Calendar cal = Calendar.getInstance();
		try
		{
			this.sqlMapClient.startTransaction();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("itemId", actRecord.getItemId());
			map.put("itemName", actRecord.getItemName());
			map.put("price", actRecord.getPrice());
			map.put("recordTime", actRecord.getRecordTime());
			map.put("activityName", actRecord.getActivityName());
			map.put("activityId", actRecord.getActivityId());
			if (actRecord.getToperId() == 0)
			{
				map.put("toperId", null);
			}
			else
			{
				map.put("toperId", actRecord.getToperId());
			}
			map.put("buyerNick", actRecord.getBuyerNick());
			map.put("buyNum", actRecord.getBuyNum());
			map.put("id", cal.get(Calendar.MONTH) + 1);
			map.put("bidPrice", actRecord.getBidPrice());
			map.put("status", actRecord.getStatus());
			map.put("discount", actRecord.getDiscount());
			this.sqlMapClient.insert("activityRecord.addActivityRecord", map);
			this.sqlMapClient.update("activityRecord.updateActivityDealCount", map);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public ActivityRecordBean queryActRecordById(long id)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		Calendar cal = Calendar.getInstance();
		map.put("month", cal.get(Calendar.MONTH) + 1);
		map.put("id", id);
		try
		{
			this.sqlMapClient.startTransaction();
			Object obj = this.sqlMapClient.queryForObject("activityRecord.queryActivityRecordById", map);
			this.sqlMapClient.commitTransaction();
			return (ActivityRecordBean) obj;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<ActivityRecordBean> queryActRecordByItemId(long itemId)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		Calendar cal = Calendar.getInstance();
		map.put("month", cal.get(Calendar.MONTH) + 1);
		map.put("itemId", itemId);
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivityRecordBean> list = (List<ActivityRecordBean>) this.sqlMapClient.queryForList(
					"activityRecord.queryActivityRecordByItemId", map);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<ActivityRecordBean> queryActRecordByActId(long actId, int num)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		Calendar cal = Calendar.getInstance();
		map.put("month", cal.get(Calendar.MONTH) + 1);
		map.put("actId", actId);
		map.put("size", num);
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivityRecordBean> list = (List<ActivityRecordBean>) this.sqlMapClient.queryForList(
					"activityRecord.queryActivityRecordByActId", map);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public void delActRecordById(long id)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		Calendar cal = Calendar.getInstance();
		map.put("month", cal.get(Calendar.MONTH) + 1);
		map.put("id", id);
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.delete("activityRecord.delActivityRecordById", map);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}

	}

	@Override
	public void delActRecordByItemId(long itemId)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		Calendar cal = Calendar.getInstance();
		map.put("month", cal.get(Calendar.MONTH) + 1);
		map.put("itemId", itemId);
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.delete("activityRecord.delActivityRecordByItemId", map);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}

	}

	@Override
	public void delActRecordByActId(long actId)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		Calendar cal = Calendar.getInstance();
		map.put("month", cal.get(Calendar.MONTH) + 1);
		map.put("actId", actId);
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.delete("activityRecord.delActivityRecordByActId", map);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}

	}

	@Override
	public void modifyActRecordToperByItemId(long toperId, long itemId)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("toperId", Long.valueOf(toperId));
		map.put("itemId", Long.valueOf(itemId));
		Calendar cal = Calendar.getInstance();
		map.put("month", cal.get(Calendar.MONTH) + 1);
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.update("activityRecord.modifyActivityRecordToperByItemId", map);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public long queryActRecordByItemIdAndNick(long itemId, String nick) throws SQLException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("itemId", itemId);
		map.put("nick", nick);
		map.put("month", new Date().getMonth() + 1);
		Object result = this.sqlMapClient.queryForObject("activityRecord.queryActRecordByItemIdAndNick", map);
		if (null == result)
		{
			return 0;
		}
		return Long.parseLong(result.toString());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<ActivityRecordBean> queryUnsyncActRecordBySeller(SiteSeller seller) throws SQLException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("nick", seller.getNick());
		map.put("status", Constant.TAG_USER_NOT_ADDED);// 未同步
		map.put("month", new Date().getMonth() + 1);
		return this.sqlMapClient.queryForList("activityRecord.queryUnsyncActRecordBySeller", map);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void updateSyncStatus(ActivityRecordBean deal) throws SQLException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", deal.getId());
		map.put("status", Constant.TAG_USER_ADDED);// 已同步
		map.put("month", new Date().getMonth() + 1);
		this.sqlMapClient.update("activityRecord.updateSyncStatus", map);
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<AnalysisDealByDay> queryDealCountByDay(SiteSeller seller, int month) throws SQLException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("nick", seller.getNick());
		map.put("month", month);
		return this.sqlMapClient.queryForList("activityRecord.queryDealCountByDay", map);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<AnalysisDealByItem> queryDealCountByActivity(long activityId, int month) throws SQLException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("month", month);
		map.put("activityId", activityId);
		return this.sqlMapClient.queryForList("activityRecord.queryDealCountByActivity", map);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<AnalysisDealByItem> queryDealCountByItem(long itemId, int month) throws SQLException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("month", month);
		map.put("itemId", itemId);
		return this.sqlMapClient.queryForList("activityRecord.queryDealCountByItem", map);
	}

}
